﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RegexJson.Common;
namespace RegexJson
{

    public partial class frmIndex : Form
    {
        List<KeyWordInfo> qpList = new List<KeyWordInfo>();
        KeyWordSearch keyWordSearch = null;
        public frmIndex()
        {
            InitializeComponent();
           
        }

        private void btn_Start_Click(object sender, EventArgs e)
        {
            dataGridView1.Rows.Clear();
            qpList.Clear();
            string strTemp = tbContent.Text;
            string[] strs = strTemp.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            btn_Start.Enabled = false;
            btnStop.Enabled = true;
            foreach (string str in strs)
            {
                KeyWordInfo keword = new KeyWordInfo();
                keword.KeyWord = str;
                qpList.Add(keword);
            }
            if (keyWordSearch == null)
            {
                keyWordSearch = new KeyWordSearch(qpList);
                keyWordSearch.KeyWordSearchHandle += new KeyWordSearch.KeyWordSearchEventHandle(keyWordSearchUpdate);
            }
            keyWordSearch.Start();
        }

        private void keyWordSearchUpdate(object sender, KeyWordSearchArgs e)
        {
           
            this.Invoke((EventHandler)delegate
            {
                if (e.IsEnd == true)
                {
                    btn_Start.Enabled = true;
                    btnStop.Enabled = false;
                    keyWordSearch.Stop();
                    keyWordSearch.Dispose();
                }
                else
                {
                    if (e.CountNum > 0)
                    {
                        lbCountNum.Text = e.CountNum.ToString();
                    }
                    else
                    {
                        DataGridViewRow dr = new DataGridViewRow();

                        foreach (DataGridViewColumn c in this.dataGridView1.Columns)
                        {
                            dr.Cells.Add(c.CellTemplate.Clone() as DataGridViewCell);  //给行添加单元格
                        }
                        lbIndexNum.Text = e.IndexNum.ToString();
                        dr.Cells[0].Value = e.IndexNum;
                        dr.Cells[1].Value = e.KeyWord;
                        dr.Cells[2].Value = e.JingJiaNum;
                        dr.Cells[3].Value = e.BaiduIndexNum;
                        dr.Cells[4].Value = e.GoogleIndexNum;
                        this.dataGridView1.Rows.Add( dr);
                    }
                }
            });
           
           
           
        }
        
        private void Form2_Load(object sender, EventArgs e)
        {
            
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            btnStop.Enabled = false;
            btn_Start.Enabled = true;
            if (keyWordSearch != null)
                keyWordSearch.Stop();
        }
    }
}
